package com.fieldchain.ex.fa100.ui.personal.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.BankCardEntity;
import com.fieldchain.ex.fa100.bean.User;
import com.google.gson.Gson;

import java.util.HashMap;

import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/12
 */
public class AddBankActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private RelativeLayout reaCheckbank;
    private TextView tvBankname;
    private EditText etBank;
    private EditText etBankid;
    private EditText etPhone;
    private EditText etPwd;
    private TextView tvSure;
    private String name = "";

    private User user;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        reaCheckbank = findViewById(R.id.rea_checkbank);
        tvBankname = findViewById(R.id.tv_bankname);
        etBank = findViewById(R.id.et_bank);
        etBankid = findViewById(R.id.et_bankid);
        etPhone = findViewById(R.id.et_phone);
        etPwd = findViewById(R.id.et_pwd);
        tvSure = findViewById(R.id.tv_sure);
    }

    @Override
    public void setValue() {
        tvCenter.setText("添加银行卡");
        user = ShareManager.getInstance().getUser();
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        reaCheckbank.setOnClickListener(this);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_addbank;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.rea_checkbank:
                Intent intent = new Intent(mContext, BankChoseActivity.class);
                intent.putExtra("name", name);
                startActivityForResult(intent, 200);
                break;
            case R.id.tv_sure:
                report();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            name = data.getStringExtra("name") + "";
            tvBankname.setText(name);
        }
    }

    public void report() {
        String bank = tvBankname.getText().toString();
        String branch = etBank.getText().toString();
        String bankCard = etBankid.getText().toString();
        String phone = etPhone.getText().toString();
        String pwd = etPwd.getText().toString();


        if (bank.length() == 0) {
            MyToast.show("请选择开户行");
            return;
        }

        if (branch.length() == 0) {
            MyToast.show("请输入支行");
            return;
        }

        if (bankCard.length() == 0) {
            MyToast.show("请输入银行卡号");
            return;
        }

        if (phone.length() == 0) {
            MyToast.show("请输入手机号码");
            return;
        }

        if (pwd.length() == 0) {
            MyToast.show("请输入交易密码");
            return;
        }


        BankCardEntity entity = new BankCardEntity();
        entity.setBank(bank);
        entity.setBankCard(bankCard);
        entity.setBranch(branch);
        entity.setHolder(user.getTrueName());
        entity.setPhone(phone);
        entity.setRemark("");
        String json = new Gson().toJson(entity);

        HashMap map = new HashMap();
        map.put("tpw", pwd);
        map.put("phone", phone);
        map.put("bankCardEntity", json);
        HttpRequest.getInstance().postRequest(json, Constant.addbank+"?tpw="+pwd+"&phone="+phone,new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                if(code==200){
                    MyToast.show("添加成功");
                    finish();
                }else {
                    MyToast.show(msg);
                }
            }

            @Override
            public void onFaild(Exception e) {
                Log.e("11111111111", e.toString());
            }
        });

    }
}
